package plus.lgx.textsearcher.searcher.extender;

import plus.lgx.textsearcher.config.SearcherConfig;

/**
 * Class name: MultilineExtender
 *
 * Create time: 12/24/22 3:47 PM
 *
 * @author lgx
 * @version 1.0
 */
@Deprecated
public class MultilineExtender extends Extender {

    public MultilineExtender(SearcherConfig config) {
        super(config);
    }

    private final int maxExtendCharsCount = config.maxExtendCharsCount;
    private final int lines = config.lines;

    @Override
    public String extend() {
        int beginInd = Math.max(index.get() - 1, 0),
            endInd   = Math.min(index.get() + 1, off - 1),
            l;
        char b, e;

        for (b = content.charAt(beginInd), l = 0;
             beginInd > 0 && index.get() - beginInd <= maxExtendCharsCount && lines - l >= 0;
             b = content.charAt(--beginInd))
            if (b == '\n')
                l++;

        for (e = content.charAt(endInd), l = 0;
             endInd < off - 1 && endInd - index.get() <= maxExtendCharsCount && lines - l >= 0;
             e = content.charAt(++endInd))
            if (e == '\n')
                l++;

        index.set(Math.min(endInd, off));
        return content.substring(Math.min(beginInd + 2, off), Math.min(endInd, off)).trim();
    }

}
